#ifndef _CLASSOBJECT_HPP
#define _CLASSOBJECT_HPP

#include <string>
#include "Object.hpp"
#include "Type.hpp"
#include <map>
#include <vector>
#include "KeyVal.hpp"

namespace object {
	class ClassObject : public Object {
	public:
		ClassObject() {
			//Gc::getInstance().addObject(this);
		}

		virtual string Type() override {
			return CLASS_OBJECT;
		}

		virtual string examine() override {
			string out;
			string tab = "    ";
			Node::_depth++;
			out.append("class");
			out.append(" " + _className);
			out.append(" {\n");
			size_t member_size = _properties.size();
			if (member_size) {
				std::vector<std::pair<string, keyVal*>> args(_properties.begin(), _properties.end());
				std::sort(args.begin(), args.end(), [](auto& a, auto& b) {
					return a.first < b.first;
				});
				for (auto& itr : args) {
					for (int i = 0; i < Node::_depth; i++) {
						out.append(tab);
					}
					out.append(itr.first);
					if (itr.second->_val != nullptr) {
						out.append(" = ");
						out.append(itr.second->_val->examine());
					}
					out.append(";");
					out.append("\n");
				}
			}
			size_t method_size = _methods.size();
			if (method_size) {
				std::vector<std::pair<string, keyVal*>> args(_methods.begin(), _methods.end());
				std::sort(args.begin(), args.end(), [](auto& a, auto& b) {
					return a.first < b.first;
				});
				for (auto& itr : args) {
					out.append(itr.second->_val->examine());
					out.append("\n");
				}
			}
			out.append("}");
			Node::_depth--;
			return out;
		}

		/*virtual void addRef() override {
			_refCount++;
		}
		virtual void releaseRef() override {
			_refCount--;
			if (_refCount == 0) {
				delete this;
			}
		}*/

		virtual ~ClassObject() {
			if (_properties.size()) {
				for (auto& itr : _properties) {
					delete itr.second;
				}
				_properties.clear();
			}
			if (_methods.size()) {
				for (auto& itr : _methods) {
					delete itr.second;
				}
			}
			_properties.clear();
		}
	public:
		string _className;
		std::map<string, keyVal*> _properties;
		std::map<string, keyVal*> _methods;
	};
}


#endif // !_CLASSOBJECT_HPP
